<?php

/**
 * @author chenshubo
 * 自建供应商支付类逻辑
 */
namespace App\Service\OrderService\OrderRouteService;
use App\Service\Code\Normal as NormalModel;
use App\Service\BaseService;
use App\Model\PssModel\OrderSupplier;
use Service\AppService\IdService;

class Normal extends BaseService
{
    /**
     * 创建订单
     * @param array $param
     * @return mixed
     */
    public function createOrder( $param = [] )
    {
        //生成供应商主单号
        $supplierCode = $param['supplierGoodsInfo']['supplierCode'];
        $userGoodsList = $param['supplierGoodsInfo']['userGoodsList'];
        //重新合并每一个仓库商品
        $orderGoods = [];
        foreach ($userGoodsList as $goods ) {
            $orderGoods = array_merge($orderGoods,$goods);
            unset($goods);
        }
        
        $param['order']['goods'] = $orderGoods;
        
        //下游是否申报
        $isDelare = (
            (isset($param['order']['isOrderDeclare']) && $param['order']['isOrderDeclare'] == 1) ||
            (isset($param['order']['isPayDeclare']) && $param['order']['isPayDeclare'] == 1)
            ) ? true : false;
        
        //是否云仓商城
        $isShop = isset($param['order']['isSplitOrder']) && $param['order']['isSplitOrder'] == 1 ? true : false ;
        
        //防止订单号重复问题,下游已申报，则使用下游的订单号作为中台子单号
        if( $isDelare && !$isShop) {
            $param['order']['supplier_dsn'] = $param['order']['orderSn'];
        } else {
            $param['order']['supplier_dsn'] = IdService::instance()->getOtherID();
        }
        
        $param['order']['supplier_psn'] = OrderSupplier::instance()->getCreateOrderSn($supplierCode);
        $param['order']['supplier_csn'] = IdService::instance()->getOtherID();
        
        $res = OrderSupplier::instance()->getSupplierCreateOrderData($param);
        if( $res['code'] != NormalModel::SUC ) {
            return $res;
        }
        
        $orderItems[] = $res['data'];
        return NormalModel::returnCode(NormalModel::SUC,"获取成功",[
            'supplierCode' => $supplierCode,
            'orderItems' => $orderItems
        ]);
    }
    
    /**
     * 订单支付
     * @param array $param
     * @return mixed
     */
    public function orderPay( $param = [] )
    {
        return NormalModel::returnCode(NormalModel::SUC,"处理完成");
    }

    /**
     * 订单取消
     * @param array $param
     * @return mixed
     */
    public function orderCancel( $param = [] )
    {
        return NormalModel::returnCode(NormalModel::SUC,"处理完成");
    }

    /**
     * 订单售后
     * @param array $param
     * @return mixed
     */
    public function orderAfterSale($param = [])
    {
        return NormalModel::returnCode(NormalModel::SUC,"处理完成");
    }
}
